/* eslint-disable */

export default cc.Class({
    extends: cc.Component,
    properties: {
        vertexs: {
            default: null,
            notify: function() {
                this.update_draw = true;
            },
        },
        fillColor: {
            default: cc.color(128, 128, 128),
            notify: function() {
                this.update_draw = true;
            },
        },
    },

    onLoad() {
        this.draw();
    },

    update() {
        if (this.update_draw) {
            this.update_draw = false;
            this.node.vertexs = this.vertexs;
            this.draw();
        }
    },

    draw() {
        let vertexs = this.vertexs;
        if (!vertexs || vertexs.length < 3) {
            //console.error('[Block Component]: vertexs not found');
            return;
        }

        let g = this.node.getComponent(cc.Graphics);
        if (!g) {
            console.error('[Block Component]: cc.Graphics not found');
            return;
        }

        g.lineWidth = 0;
        g.fillColor = this.fillColor;
        g.clear();

        g.moveTo(vertexs[0].x, vertexs[0].y);
        for (let j = 1; j < vertexs.length; ++j) {
            g.lineTo(vertexs[j].x, vertexs[j].y);
        }
        g.lineTo(vertexs[0].x, vertexs[0].y);
        g.stroke();
        g.fill();
    },
});
